package com.hp.attendance.serlvet.attendance;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.service.AttendanceService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;


@WebServlet("/addStart")
public class addStartServlet extends HttpServlet {

    private AttendanceService attendanceService = new AttendanceService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {

            String employee_number = req.getParameter("employee_number");
            //获取当前时间
            LocalDateTime now = LocalDateTime.now();
            int year = now.getYear();
            int month = now.getMonthValue();
            int dayOfMonth = now.getDayOfMonth();
            int hour = now.getHour();
            int minute = now.getMinute();
            String m2="";
            if (month<10){
                String s = String.valueOf(month);
                m2="0"+s;
            }

            int second = now.getSecond();
            Attendance att = new Attendance();
            if (hour > 12) {
                att.setTime_type("下午");
            } else {
                att.setTime_type("上午");
            }
            String start_type = hour > 9 && minute > 0 ? "迟到" : "正常";
            att.setStart_type(start_type);
            att.setDay(year + "-" + m2 + "-" + dayOfMonth);
            att.setStart_time(hour + ":" + minute + ":" + second);
            att.setEmployee_number(Integer.valueOf(employee_number));
            Attendance attendance = attendanceService.queryAttByNumDay(Integer.parseInt(employee_number), year + "-" + m2 + "-" + dayOfMonth);
            if (attendance.getDay()==null){
                attendanceService.addStartAttend(att);
            }else {
                req.setAttribute("msg","已打卡无法重复打卡");
            }
            req.getRequestDispatcher("/welcome").forward(req, resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
